﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cn.Cellula.Biz.Port;
using Cn.Cellula.Model.System;
using System.Reflection;
using Cn.Cellula.Model.Content;
using Cn.Edwon.Spring;
using Cn.Edwon.Web.Ac;

namespace Cn.Cellula.Biz.Impl {
    class ItemBiz : MyBizSupport, IItemBiz {

        private String BuildRoleCheckHql(UserPower rPower, String rColumnName) {
            return " (" + rColumnName + " is null "
                + " or " + rColumnName + "='' "
                + " or " + rColumnName + "='" + rPower.RoleKey + "' "
                + " or " + rColumnName + "<=" + rPower.RoleLevel + ") ";
        }
        private String BuildDeptCheckHql(UserPower rPower, String rColumnName) {
            return " (" + rColumnName + " is null "
                + " or " + rColumnName + "='' "
                + " or " + rColumnName + "='" + rPower.DeptKey + "' "
                + " or " + rColumnName + "<=" + rPower.DeptLevel + ") ";
        }
        public IList<TSysAtlOptionsEO> LoadAltOptions(string rAtlKey) {
            String hql = "from TSysAtlOptionsEO eo where eo.AtlKey='" + rAtlKey + "' order by eo.OrderNumber desc";
            return base.Find<TSysAtlOptionsEO>(hql);
        }

        public void DeleteAlt(IList<string> rAltKeys) {
            foreach (String s in rAltKeys) {
                try {
                    String hql = "from TSysAtlOptionsEO eo where eo.AtlKey='" + s + "'";
                    base.Delete(hql);
                    base.Delete<TSysAtlEO>(s);
                } catch { }
            }
        }

        public void DeleteAltOption(IList<string> rOptKeys) {
            foreach (String s in rOptKeys) {
                try {
                    base.Delete<TSysAtlOptionsEO>(s);
                } catch { }
            }
        }
        public IList<VItemListEO> LoadItemByCategory(string rCategoryKey,String rOwnerKey, UserPower rPower) {
            String hql = "from VItemListEO eo ";
            hql += " where eo.OwnerKey='" + rOwnerKey + "' ";
            hql += " and " + BuildRoleCheckHql(rPower, "eo.VisiableRoleLevel");
            hql += " and " + BuildDeptCheckHql(rPower, "eo.DeptLevel");

            ICategoryBiz biz = BizFactory.Get<ICategoryBiz>();
            IList<String> ckeys = biz.LoadDescendantsKeys(rOwnerKey, null, rCategoryKey);
            if (ckeys != null && ckeys.Count > 0) {
                hql += " and (";
                hql += "  eo.CategoryKey='" + rCategoryKey + "' "; 
                for (int i = 0; i < ckeys.Count; i++) {
                    hql += " or eo.CategoryKey='" + ckeys[i] + "' ";
                }
                hql += " ) ";
            } else { 
                hql += " and eo.CategoryKey='" + rCategoryKey + "' ";
            }
            hql += " order by eo.CategoryKey,eo.ItemName ";
            return base.Find<VItemListEO>(hql);
        }
        public IList<VSysAtlEO> LoadSysAtlByQuery(string rCategoryKey,UserPower rPower) {
            String hql = "from VSysAtlEO eo ";
            hql += " where eo.CategoryKey='" + rCategoryKey + "' ";
            hql += " and " + BuildRoleCheckHql(rPower, "eo.VisiableRoleLevel");
            hql += " and eo.IsQueriable=1 ";
            //不搜索时间类型的ATL
            hql += " and eo.FieldType<>'"+CodeTypeDef.FormField.DateTime +"' ";
            hql += " and eo.FieldType<>'"+CodeTypeDef.FormField.Date +"' ";

            ICategoryBiz biz = BizFactory.Get<ICategoryBiz>();
            IList<TSysCategoryEO> ckeys = biz.LoadCategoryPath(null, null, rCategoryKey);
            if (ckeys != null && ckeys.Count > 0) {
                for (int i = 0; i < ckeys.Count; i++) {
                    TSysCategoryEO k = ckeys[i];
                    hql += " or eo.CategoryKey='" + k.CategoryId + "' ";
                }
            }
            hql += " order by eo.CategoryKey,eo.OrderNumber desc ";
            return base.Find<VSysAtlEO>(hql);
        }


        public void SaveProductAtlData(IList<TItemAtlDataEO> rDataList) {
            if (rDataList == null || rDataList.Count < 1) return;
            String itemKey = rDataList[0].ItemKey;
            String hql = " from TItemAtlDataEO eo where eo.ItemKey='" + itemKey + "' ";
            base.Delete(hql);
            foreach (TItemAtlDataEO eo in rDataList) {
                base.Save<TItemAtlDataEO>(eo);
            }
        }

        public IList<VSysAtlOptionsEO> LoadItemOptByCategory(string rCategoryKey) {
            String hql = "from VSysAtlOptionsEO eo ";
            hql += " where eo.CategoryKey='" + rCategoryKey + "' ";
            ICategoryBiz biz = BizFactory.Get<ICategoryBiz>();
            IList<TSysCategoryEO> ckeys = biz.LoadCategoryPath(null, null, rCategoryKey);
            if (ckeys != null && ckeys.Count > 0) {
                for (int i = 0; i < ckeys.Count; i++) {
                    TSysCategoryEO k = ckeys[i];
                    hql += " or eo.CategoryKey='" + k.CategoryId + "' ";
                }
            }
            hql += " order by eo.CategoryKey,eo.OrderNumber desc ";
            return base.Find<VSysAtlOptionsEO>(hql);
        }
        public void DeleteItem(IList<String> rKeys) {
            foreach (String s in rKeys) {
                TItemEO eo = base.Load<TItemEO>(s);
                //设置删除标记
                eo.FlgDel = 1;
                //被删除的商品将无分类
                //eo.CategoryId = "";
                base.Update<TItemEO>(eo);

                //删除模板属性关联
                try {
                    String hql = "from TItemAtlDataEO eo where eo.ItemKey='" + eo.ItemKey + "'";
                    base.Delete(hql);
                } catch (Exception ex) {
                    log.Error("删除商品-删除其模板属性值时出现异常:" + ex.Message);
                }

            }
        }
        public IList<VSysAtlEO> LoadSysAtlByCategory(string rCategoryKey,UserPower rPower) {
            String hql = "from VSysAtlEO eo ";
            hql += " where eo.CategoryKey='" + rCategoryKey + "' ";
            hql += " and " + BuildRoleCheckHql(rPower, "eo.VisiableRoleLevel");

            ICategoryBiz biz = BizFactory.Get<ICategoryBiz>();
            IList<TSysCategoryEO> ckeys = biz.LoadCategoryPath(null, null, rCategoryKey);
            if (ckeys != null && ckeys.Count > 0) {
                for (int i = 0; i < ckeys.Count; i++) {
                    TSysCategoryEO k = ckeys[i];
                    hql += " or eo.CategoryKey='" + k.CategoryId + "' ";
                }
            }
            hql += " order by eo.CategoryKey,eo.OrderNumber desc ";
            return base.Find<VSysAtlEO>(hql);
        }
        public IList<VSysAtlOptionsEO> LoadSysAtlOptionsByCategory(string rCategoryKey) {
            String hql = "from VSysAtlOptionsEO eo ";
            hql += " where eo.CategoryKey='" + rCategoryKey + "' ";
            ICategoryBiz biz = BizFactory.Get<ICategoryBiz>();
            IList<TSysCategoryEO> ckeys = biz.LoadCategoryPath(null, null, rCategoryKey);
            if (ckeys != null && ckeys.Count > 0) {
                for (int i = 0; i < ckeys.Count; i++) {
                    TSysCategoryEO k = ckeys[i];
                    hql += " or eo.CategoryKey='" + k.CategoryId + "' ";
                }
            }
            hql += " order by eo.CategoryKey,eo.OrderNumber desc ";
            return base.Find<VSysAtlOptionsEO>(hql);
        }

        public VItemListEO LoadItem(string rKey,String rOwnerKey, UserPower rPower) {
            String hql = "from VItemListEO eo ";
            hql += " where eo.ItemKey='" + rKey + "' ";
            hql += " and eo.OwnerKey='" + rOwnerKey + "' ";
            hql += " and " + BuildRoleCheckHql(rPower, "eo.VisiableRoleLevel");
            hql += " and " + BuildDeptCheckHql(rPower, "eo.DeptLevel");
            IList<VItemListEO> list = base.Find<VItemListEO>(hql);
            if(list!=null && list.Count>0){
                return list[0];
            }else{
                return null;
            }
        }

        public VItemDetailsEO LoadItemDetails(string rKey,String rOwnerKey, UserPower rPower) {
             String hql = "from VItemDetailsEO eo ";
            hql += " where eo.ItemKey='"+rKey+"' ";
            hql += " and eo.OwnerKey='" + rOwnerKey + "' ";
            hql += " and " + BuildRoleCheckHql(rPower, "eo.VisiableRoleLevel");
            hql += " and " + BuildDeptCheckHql(rPower, "eo.DeptLevel");
            IList<VItemDetailsEO> list = base.Find<VItemDetailsEO>(hql);
            if(list!=null && list.Count>0){
                return list[0];
            }else{
                return null;
            }        }

        public VSysAtlEO LoadAtl(string rKey, UserPower rPower) {
            String hql = "from VSysAtlEO eo ";
            hql += " where eo.AtlKey='"+rKey+"' ";
            hql += " and " + BuildRoleCheckHql(rPower, "eo.VisiableRoleLevel");
            IList<VSysAtlEO> list = base.Find<VSysAtlEO>(hql);
            if(list!=null && list.Count>0){
                return list[0];
            }else{
                return null;
            }
        }
        public VItemAtlDataEO LoadAtlData(String rKey, String rOwnerKey, UserPower rPower){
            String hql = "from VItemAtlDataEO eo ";
            hql += " where eo.OwnerKey='" + rOwnerKey + "' ";
            hql += " and " + BuildRoleCheckHql(rPower, "eo.AtlVisiableRoleLevel");
            hql += " and " + BuildDeptCheckHql(rPower, "eo.ItemDeptLevel");
            IList<VItemAtlDataEO> list = base.Find<VItemAtlDataEO>(hql);
            if (list != null && list.Count > 0) {
                return list[0];
            } else {
                return null;
            }
        }

        public IList<VItemAtlDataEO> LoadAtlDataByItem(String rItemKey, String rOwnerKey, UserPower rPower) {
            String hql = " from VItemAtlDataEO eo where eo.ItemKey='" + rItemKey + "' ";
            hql += " and eo.OwnerKey='" + rOwnerKey + "' ";
            hql += " and " + BuildRoleCheckHql(rPower, "eo.AtlVisiableRoleLevel");
            hql += " order by eo.AtlOrderNumber desc ";
            return base.Find<VItemAtlDataEO>(hql);
        }

        public IList<VItemAtlDataEO> LoadAtlDataByCategory(String rCategoryKey, String rOwnerKey, UserPower rPower) {
            String hql = " from VItemAtlDataEO eo where eo.OwnerKey='" + rOwnerKey + "' ";
            hql += " and " + BuildRoleCheckHql(rPower, "eo.AtlVisiableRoleLevel");
            IList<String> list = BizFactory.Get<ICategoryBiz>().LoadDescendantsKeys(rOwnerKey, null, rCategoryKey);
            if (list != null) {
                hql += " and eo.CategoryKey in (";
                for(int i=0;i<list.Count;i++){
                    String c = list[i];
                    if (i > 0)
                        hql += ",";
                    hql += "'" + c + "'";
                }
                hql += " ) ";
            }
            hql += " order by eo.AtlOrderNumber desc ";
            return base.Find<VItemAtlDataEO>(hql);
        }
    }
}
